package com.hgyc.mom.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.system.entity.SysRole;
import com.hgyc.mom.system.service.SysRoleMenuService;
import com.hgyc.mom.system.service.SysRoleService;
import com.hgyc.mom.system.vo.RoleMenuVO;
import com.hgyc.mom.system.vo.SysRoleParam;
import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统角色信息 前端控制器
 * </p>
 *
 * @author fwj
 * @since 2025-01-16
 */
@RestController
@RequestMapping("/sys/role")
@Tag(name = "系统角色接口", description = "系统角色接口")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result<IPage<SysRole>> list(@RequestBody SysRoleParam sysRoleParam) {
        Page<SysRole> page = new Page<>(sysRoleParam.getPage(), sysRoleParam.getPageSize());
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleParam, sysRole);
        QueryWrapper<SysRole> wrapper = QueryWrapperUtil.build(sysRole);
        IPage<SysRole> list = sysRoleService.page(page, wrapper);
        return Result.success(list);
    }

    @RequestMapping(value = "/allRole", method = RequestMethod.GET)
    public Result<List<SysRole>> allRole() {
        List<SysRole> roles = sysRoleService.allRole();
        return Result.success(roles);
    }

    @RequestMapping(value = "/getRole", method = RequestMethod.GET)
    public Result<SysRole> queryUserInfo(@RequestParam("roleId") String roleId) {
        SysRole sysRole = sysRoleService.getById(roleId);
        return Result.success(sysRole);
    }

    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    public Result<?> batchDel(@RequestParam("ids") List<Long> ids) {
        sysRoleService.removeBatchByIds(ids);
        return Result.success("删除成功");
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result<?> add(@RequestBody SysRole sysRole) {
        sysRoleService.save(sysRole);
        return Result.success("保存成功");
    }

    @RequestMapping(value = "/editRole", method = RequestMethod.POST)
    public Result<?> edit(@RequestBody SysRole sysRole) {
        sysRoleService.updateById(sysRole);
        return Result.success("保存成功");
    }

    @RequestMapping(value = "/updateRoleResource", method = RequestMethod.POST)
    public Result<?> updateRoleResource(@RequestBody RoleMenuVO roleMenuVO) {
        sysRoleMenuService.updateRoleResource(roleMenuVO);
        return Result.success("更新成功");
    }

}
